package com.bjsxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.domain.TradeArea;
import com.bjsxt.dto.TradeAreaDto;
import com.bjsxt.model.R;
import com.bjsxt.service.TradeAreaService;
import com.bjsxt.vo.TradeAreaMarketVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/tradeAreas")
public class TradeAreaController {

    @Autowired
    private TradeAreaService tradeAreaService;

    @GetMapping
    @PreAuthorize("hasAuthority('trade_area_query')")
    public R<Page<TradeArea>> findByPage(Page<TradeArea>page,String name,Byte status){
    Page<TradeArea> page1 =     tradeAreaService.findByPage(page,name,status);
    return R.ok(page1);
    }

    @PostMapping
    public R save(@RequestBody TradeArea tradeArea){


     boolean isOk =    tradeAreaService.save(tradeArea);
        if(isOk){
            return R.ok();
        }

       return R.fail();

    }




    @PatchMapping
    public R update(@RequestBody TradeArea tradeArea){


        boolean isOk =    tradeAreaService.updateById(tradeArea);
        if(isOk){
            return R.ok();
        }

        return R.fail("修改失败");

    }



    @PostMapping("/status")
    public R updateStatus(@RequestBody TradeArea tradeArea){


        boolean isOk =    tradeAreaService.updateById(tradeArea);
        if(isOk){
            return R.ok();
        }

        return R.fail("修改失败");

    }





    @GetMapping("/all")
    @ApiOperation(value = "查询交易区域")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "交易区域的状态")
    })
    @PreAuthorize("hasAuthority('trade_area_query')")
    public R<List<TradeArea>> findAll(Byte status) {
        List<TradeArea> tradeAreas = tradeAreaService.findAll(status);
        return R.ok(tradeAreas);
    }


    @GetMapping("/markets")
    @ApiOperation(value = "查询交易区域,以及区域下的市场")
    public R<List<TradeAreaMarketVo>> getTradeAreaMarkets(){
        List<TradeAreaMarketVo> tradeAreaMarketVos =  tradeAreaService.findTradeAreaMarket();
        return R.ok(tradeAreaMarketVos) ;
    }

    @GetMapping("/market/favorite")
    @ApiOperation(value = "用户收藏的交易市场")
    public R<List<TradeAreaMarketVo>> getUserFavoriteMarkets(){
        Long userId =  Long.valueOf(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        List<TradeAreaMarketVo> tradeAreaMarketVos =  tradeAreaService.getUserFavoriteMarkets(userId) ;
        return R.ok(tradeAreaMarketVos) ;
    }

    /**
     * 查询所有的交易区域
     *
     * @return
     */

    public List<TradeAreaDto> tradeAreas() {
        List<TradeAreaDto> tradeAreaDtoList =  tradeAreaService.findAllTradeAreaAndMarket();
        return tradeAreaDtoList ;
    }




}
